---
title: "Part 3: Deconstruction Wood Sensitivity Analysis"
subtitle: "Transport Analysis using OSU average softwood density factor"
author:
- Andey Nunes, MS
- Research Analyst 2
- Oregon DEQ
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
   html_document:
    df_print: paged
    toc: yes
    toc_depth: '3'
---

```{r notebook setup, echo = F, warning = F}
packages <- c("knitr", "readxl", "rebus", "scales", "tidyverse")
lapply(packages, require, character.only = T)
options(digits = 3)
options(scipen = 999)
```

# LCA Impacts on Material Transport for Deconstruction

> Add discussion about data sources here.

In the following code blocks, the `Activity` refers to the scenarios: `decon` for deconstruction, and `demo` for demolition.


## Transport Data 
```{r import data,  warning = F}
# data for generating transport impact values
routes <- read_csv("data/TransportRoutes.csv")

house_weight <- read_csv("intermediary/house_weight.csv")

number_of_projects <- as.numeric(length(house_weight$project))

# data for material based transport impacts
material_name_mapping <- read_csv("intermediary/deconMaterialName_SimpleEOLname_mapping.csv")

# dropbox data for calculating dropbox transport
dropbox_EOL_weight <- read_csv("intermediary/dropbox_EOL_weight_composition.csv")

# impact data for combining final results
decon <- read_csv("intermediary/deconData.csv")

demo <- read_csv("intermediary/demoData.csv")
```



## Project Based Values

The transport calculation on a project basis involves workers for both scenarios. For the demolition projects there are also equipment impacts. 

**Formula: project based impacts**
*for each Activity, multiply the Carbon or Energy `...PerProjectImpacts` by the number of projects*



```{r per project transport}
projects <- distinct(select(decon, c("house_age", "house_size", "project", "contractor")))

# create the labor & equipment tables and save for later
# combine per project worker transport to the larger material based impact data frames later

per_project_worker_equipment <- bind_rows(projects, projects) %>%
   add_column(decon_workers = c(rep(as.numeric(routes[1,11]),number_of_projects), rep(as.numeric(routes[1,12]),number_of_projects))) %>%
   add_column(demo_workers = c(rep(as.numeric(routes[9,11]),number_of_projects), rep(as.numeric(routes[9,12]),number_of_projects))) %>%
   add_column(demo_equipment = c(rep(as.numeric(routes[14,11]),number_of_projects), rep(as.numeric(routes[14,12]),number_of_projects))) %>%
   add_column(impact_units = c(rep("kg CO2e", number_of_projects), rep("MJ", number_of_projects) ))

write.csv(per_project_worker_equipment, "intermediary/per_project_worker_equipment_transport_impacts.csv")
```




## Material Weight Based Values
To calculate impacts of the transport of materials from the projects under the two activity scenarios, the weight of the materials and the materials destinations based on the EOL dispositions must be folded into the equation. For each scenario, there are two tranpsort legs that need to be modeled, first: from site to either retail or transfer station, and second: from transfer station to the *end use* (EOL) disposition (post-retail transport is beyond the bounds of this analysis). Distances between the projects and the material transfer and the  EOL destination (recycler, landfill, or burner) are incorporated into the transport impact values. The following data tables are therefore project, material, and dipsosition specific in order to answer questions regarding the contributions of transport impacts on the activity scenarios. The following general formula will be used to calculate the material based transport impacts:
  
**Formula: material based impacts**
*for each Activity, multiply the Carbon or Energy `Impacts` by weight of the given material denoted by the `SimpleEOLname`*

The material transport factors for the second transport leg (from MRF transfer to final EOL disposition) is the same for the decon and demo activity scenarios, therefore a table of transfer to EOL values will be used for joining to the scenario data frames in order to calculate impacts by material weight based on the EOL assignment. The `transfer_to_EOL` object will be used for the decon dropbox fate as well as the second transport leg for all materials in the demo scenario.


```{r route specific EOL transport impacts}
transfer_to_EOL <- routes %>%
   select("SimpleEOLname", "TransportLeg", "CarbonImpact", "EnergyImpact") %>%
   # pull out just the recoverable wood, metal, 
   slice(c(4:6)) %>% 
   add_row(SimpleEOLname = "nonrecoverable wood", TransportLeg = "transfer to landfill", CarbonImpact = as.numeric(routes[6,9]), EnergyImpact = as.numeric(routes[6,10]))

```

The site to transfer station transport impacts specific to the deconstruction dropbox receipts are calculated once and then used for both scenarios. 

### Dropbox Transport Impacts
```{r dropbox transport}
# first leg: dropbox (site) to transfer station
# start with dropbox weight leaving off the index and build out columns
dropbox_EOL <- dropbox_EOL_weight[,-1] %>%
   add_column(carbon_impacts_per_kg_material_to_transfer = as.numeric(routes[3,9])) %>%
   add_column(energy_impacts_per_kg_material_to_transfer = as.numeric(routes[3,10])) %>%
   mutate(carbon_impacts_dropbox_to_transfer_kgCO2e = dropbox_EOL_kg * carbon_impacts_per_kg_material_to_transfer) %>%
   mutate(energy_impacts_dropbox_to_transfer_MJ = dropbox_EOL_kg * energy_impacts_per_kg_material_to_transfer)

# second leg: dropbox transfer station to EOL disposition
dropbox_EOL <- dropbox_EOL %>%
   left_join(transfer_to_EOL, by = "SimpleEOLname") %>%
   mutate(carbon_impacts_transfer_to_EOL_kgCO2e = dropbox_EOL_kg * CarbonImpact) %>%
   mutate(energy_impacts_transfer_to_EOL_MJ = dropbox_EOL_kg * EnergyImpact)

rm(dropbox_EOL_weight)
write.csv(dropbox_EOL, "intermediary/dropbox_EOL_transport_impacts.csv")
```

This final table saved for the calculation of dropbox materials, titled `dropbox_EOL_transport_impacts.csv` contains the following information:
* `project` and `total_dropbox_quantity` in kilograms 
* associated `SimpleEOLname` and `deconMaterialName` for cross referencing materials and material fate
* `Distribution` which is the assigned waste composition of each projects' dropbox materials. The product of the `total_dropbox_quantity` and the `Distribution` is the `kg_composition` value.
* the `disposition_assignment` is used to reference the `disposition` `percentage`, which in turn is used to assign the `percentage` of the `kg_composition` value for the fate of that material, which yields the `dropbox_EOL_kg`.
* `dropbox_EOL_kg` is the weight value column used for calculating the transport leg impacts,  which are labeled according to impact category and transport leg.
* Final results for the two transport legs (site-to-transfer and transfer-to-EOL) are located in the following columns: `carbon_impacts_dropbox_to_transfer_kgCO2e`, `energy_impacts_dropbox_to_transfer_MJ`, `carbon_impacts_transfer_to_EOL_kgCO2e`, `energy_impacts_transfer_to_EOL_MJ`.

Transport impacts must still be organized by project, transport leg, and material type (`deconMaterialName`) before combining with the activity scenario data frames.

```{r gathered dropbox transport}
dropbox_site_transport <- dropbox_EOL %>%
   select("contractor", "project", "total_dropbox_quantity", "quantity_units", "SimpleEOLname", "deconMaterialName", "Distribution", "disposition_assignment", "percentage", "disposition",  "kg_composition", "dropbox_EOL_kg", "carbon_impacts_per_kg_material_to_transfer", "energy_impacts_per_kg_material_to_transfer", "carbon_impacts_dropbox_to_transfer_kgCO2e", "energy_impacts_dropbox_to_transfer_MJ") %>%
   rename(kgCO2e = carbon_impacts_dropbox_to_transfer_kgCO2e) %>%
   rename(MJ = energy_impacts_dropbox_to_transfer_MJ) %>%
   gather(`kgCO2e`, `MJ`, key = dropbox_to_transfer_units, value = dropbox_to_transfer) %>%
   select("contractor", "project", "SimpleEOLname", "deconMaterialName", "disposition", "dropbox_EOL_kg", "dropbox_to_transfer_units", "dropbox_to_transfer")

dropbox_EOL_transport <- dropbox_EOL %>%
   select("contractor", "project", "total_dropbox_quantity", "quantity_units", "SimpleEOLname", "deconMaterialName", "Distribution", "disposition_assignment", "percentage", "disposition",  "kg_composition", "dropbox_EOL_kg", "TransportLeg", "CarbonImpact", "EnergyImpact", "carbon_impacts_transfer_to_EOL_kgCO2e", "energy_impacts_transfer_to_EOL_MJ") %>%
   rename(kgCO2e = carbon_impacts_transfer_to_EOL_kgCO2e) %>%
   rename(MJ = energy_impacts_transfer_to_EOL_MJ) %>%
   gather(`kgCO2e`, `MJ`, key = transfer_to_EOL_units, value = transfer_to_EOL) %>%
   select("contractor", "project", "SimpleEOLname", "deconMaterialName", "disposition", "dropbox_EOL_kg", "transfer_to_EOL_units", "transfer_to_EOL")
```

   
   
### Deconstruction Scenario

For the first transportation leg, all salvaged materials are going from the project site to the retailer. For salvaged materials that go to retail, there is no second transport leg, however, due to the need to model comparible scenario activity a column of zeros are added to the salvage material transport impacts as place holders for the second transport leg. 
```{r decon transport}

decon$material_impact_units <- str_replace(decon$material_impact_units, " ", "")

decon_transport <- decon %>%
   rename(impact_units = material_impact_units) %>%
   mutate(site_to_retail_transport_impact = case_when(
      disposition == "reuse" & impact_units == "kgCO2e" ~ quantity * as.numeric(routes[2,9]),
      disposition == "reuse" & impact_units == "MJ" ~ quantity * as.numeric(routes[2,10]))
      ) %>%
   left_join(dropbox_site_transport, by = c("project", "contractor", "deconMaterialName", "disposition", "SimpleEOLname", c("quantity" = "dropbox_EOL_kg"), c("impact_units" = "dropbox_to_transfer_units") )) %>%
   left_join(dropbox_EOL_transport, by = c("project", "contractor", "deconMaterialName", "disposition", "SimpleEOLname", c("quantity" = "dropbox_EOL_kg"), c("impact_units" = "transfer_to_EOL_units") )) %>%
   mutate(site_transport_impact = if_else(
      disposition == "reuse", .$site_to_retail_transport_impact, .$dropbox_to_transfer)) %>%
   mutate(EOL_transport_impact = if_else(
      disposition == "reuse", 0, .$transfer_to_EOL
   )) %>%
   select("house_age", "house_size", "project", "contractor", "deconMaterialName", "SimpleEOLname", "disposition", "quantity", "quantity_units", "material_impacts", "site_transport_impact", "EOL_transport_impact", "impact_units")


# save this file for the final net benefits calculation and for exploratory graphics
write.csv(decon_transport, "intermediary/final_deconstruction_scenario_with_transport.csv")
```


### Demolition Scenario

Under the demolition scenario we model the salvaged materials as if they were all delivered by a class 8 vehicle in a dropbox to a transfer station, and from the transfer station by a class 8 vehicle to the disposition, which is either the recovery end use or landfill. The EOL data is used to redistribute the recovery of (salvaged) materials as if the materials had arrived in a combined dropbox at a transfer station and was recovered according to Metro's regional recovery rates.

```{r demo transport}

# first need transfer_to_EOL reshaped
transfer_to_EOL <- gather(transfer_to_EOL, `CarbonImpact`, `EnergyImpact` , key = "impact_type", value = "impact_value") %>%
   add_column(impact_units = c(rep("kg CO2e", 4), rep("MJ", 4)))

demo_transport <- filter(demo, material_impacts != 0) %>%
   rename(impact_units = material_impact_units) %>%
   mutate(site_transport_impact = case_when(
      impact_units == "kg CO2e" ~ quantity * as.numeric(routes[3,9]),
      impact_units == "MJ" ~ quantity * as.numeric(routes[3,10]))) %>%
   left_join(transfer_to_EOL, by = c("SimpleEOLname", "impact_units") ) %>%
   mutate(EOL_transport_impact = quantity * impact_value) %>%
   select("house_age", "house_size", "project", "contractor", "deconMaterialName", "SimpleEOLname", "disposition", "quantity", "quantity_units", "material_impacts", "site_transport_impact", "EOL_transport_impact", "impact_units")
      
# save this file for the final net benefits calculation and for exploratory graphics
write.csv(demo_transport, "intermediary/final_demolition_scenario_with_transport.csv")

```

